package com.android.nfctouch.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.dialog.Comm_DialogFragment;
import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.util.GlideUtil;
import com.android.nfctouch.R;
import com.android.nfctouch.bean.ContactInfo;
import com.android.nfctouch.constants.Constants;
import com.android.nfctouch.helper.OnItemMoveListener;
import com.android.nfctouch.manager.API_HomeManager;
import com.android.nfctouch.manager.API_Manager;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2018/8/22.
 */

public class ContactManagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnItemMoveListener {

    Context mContext;
    List<ContactInfo> list;
    CommCallBack callBack;

    public ContactManagerAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setData(List<ContactInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_mycontactmanager_list, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final ContactInfo info = list.get(position);
        //数据绑定逻辑
        viewHolder.tv_text.setText(info.sysLinkEntity.name);
        GlideUtil.displayImage(mContext, info.sysLinkEntity.url, viewHolder.iv_img, -1);
        if (info.status == 0) {
            viewHolder.tv_state.setText("所有人可见");
        } else {
            viewHolder.tv_state.setText("仅自己可见");
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onResult(info);
                }
            }
        });
        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Comm_DialogFragment.showCommDialog((FragmentActivity) mContext, "", "确定删除", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        delete(info);
                    }
                }, null);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list, fromPosition, toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }


    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_text, tv_state;
        ImageView iv_img,iv_delete;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_text = itemView.findViewById(R.id.tv_text);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_state = itemView.findViewById(R.id.tv_state);
            iv_delete= itemView.findViewById(R.id.iv_delete);
        }
    }

    private void delete(ContactInfo info) {
        API_HomeManager.home_contact_delete(mContext, info.userLinkId, new OkHttpCallBack<BaseResponce<String>>() {
            @Override
            public void onSuccess(BaseResponce<String> baseResponce) {
                CommToast.showToast(mContext, "删除成功");
                if (callBack != null) {
                    callBack.onResult(null);
                }
            }

            @Override
            public void onFailure(BaseResponce<String> baseResponce) {
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }
}
